﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using System.Xml.Serialization;

namespace FPP.Models
{
    public partial class Party : EntityObject, IFppBaseModel
    {
        #region Primitive Properties

        public virtual int Id
        {
            get;
            set;
        }

        [Required(ErrorMessage = "Party Name is required")]
        [Display(Name = "Party Name")]
        public virtual string Name
        {
            get;
            set;
        }

        public virtual int OwnerId
        {
            get;
            set;
        }

        #endregion

        #region Associated Functions

        public User Owner()
        {
            User _owner = new User();

            using (fppEntities context = new fppEntities(FPP.Controllers.DataAccess.ffpCString))
            {
                context.Connection.Open();

                _owner = context.Users.FirstOrDefault(i => i.Id == OwnerId);
            }

            return _owner;
        }

        public bool AddUser(int userid)
        {
            var up = new UserParty();
            var rtn = true;
            
            up.PartyId = Id;
            up.UserId = userid;
            up.DateJoined = DateTime.Now;

            fppEntities qCon = new fppEntities(FPP.Controllers.DataAccess.ffpCString);
            try
            {
                using (qCon)
                {
                    qCon.UserParties.AddObject(up);
                    qCon.SaveChanges(SaveOptions.None);
                }
            }
            catch
            {
                rtn = false;
                ErrorMessage = string.Format("User is already in {0}", Name);
            }

            return rtn;
        }

        public bool RemoveUser(int userid)
        {
            UserParty up;
            var rtn = true;

            fppEntities qCon = new fppEntities(FPP.Controllers.DataAccess.ffpCString);
            try
            {
                using (qCon)
                {
                    up = qCon.UserParties.First(u => u.UserId == userid);
                    qCon.UserParties.DeleteObject(up);
                    qCon.SaveChanges(SaveOptions.None);
                }
            }
            catch
            {
                rtn = false;
                ErrorMessage = string.Format("User was not removed from {0}", Name);
            }

            return rtn;
        }

        public List<User> Users()
        {
            var _users = new List<User>();

            using (var context = new fppEntities(FPP.Controllers.DataAccess.ffpCString))
            {
                context.Connection.Open();

                foreach (UserParty up in context.UserParties.Where(u => u.PartyId == Id).ToList())
                {
                    _users.Add(context.Users.FirstOrDefault(u => u.Id == up.UserId));
                }

            }

            return _users;
        }

        #endregion

        public string ErrorMessage
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
    }
}